/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.pojo.sqlServer.PPInventory;
import me.zhengjie.pojo.sqlServer.PPOutboundOrder;
import me.zhengjie.pojo.sqlServer.PPOutboundOrderDetail;
import me.zhengjie.repository.PPOutboundOrderRepository;
import me.zhengjie.service.PPOutboundOrderDetailService;
import me.zhengjie.service.PPOutboundOrderService;
import me.zhengjie.service.dto.PPOutboundOrderDto;
import me.zhengjie.service.dto.PPOutboundOrderQueryCriteria;
import me.zhengjie.service.mapper.PPOutboundOrderMapper;
import me.zhengjie.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author xinglin
* @date 2024-08-15
**/
@Service
@RequiredArgsConstructor
public class PPOutboundOrderServiceImpl implements PPOutboundOrderService {

    private final PPOutboundOrderRepository ppOutboundOrderRepository;
    private final PPOutboundOrderMapper ppOutboundOrderMapper;
    private final PPOutboundOrderDetailService ppOutboundOrderDetailService;

    @Override
    public PageResult<PPOutboundOrderDto> queryAll(PPOutboundOrderQueryCriteria criteria, Pageable pageable){
        Page<PPOutboundOrder> page = ppOutboundOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ppOutboundOrderMapper::toDto));
    }

    @Override
    public List<PPOutboundOrderDto> queryAll(PPOutboundOrderQueryCriteria criteria){
        return ppOutboundOrderMapper.toDto(ppOutboundOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PPOutboundOrderDto findById(Integer id) {
        PPOutboundOrder ppOutboundOrder = ppOutboundOrderRepository.findById(id).orElseGet(PPOutboundOrder::new);
        ValidationUtil.isNull(ppOutboundOrder.getId(),"PpOutboundOrder","id",id);
        return ppOutboundOrderMapper.toDto(ppOutboundOrder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PPOutboundOrder resources) {
        ppOutboundOrderRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PPOutboundOrder resources) {
        PPOutboundOrder ppOutboundOrder = ppOutboundOrderRepository.findById(resources.getId()).orElseGet(PPOutboundOrder::new);
        ValidationUtil.isNull( ppOutboundOrder.getId(),"PpOutboundOrder","id",resources.getId());
        ppOutboundOrder.copy(resources);
        ppOutboundOrderRepository.save(ppOutboundOrder);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            ppOutboundOrderRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PPOutboundOrderDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PPOutboundOrderDto ppOutboundOrder : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("出库单code", ppOutboundOrder.getOrderCode());
            map.put("出库单类型(1自动出库,2手动出库)", ppOutboundOrder.getOrderType());
            map.put("出库单状态(0未开始，1执行中，2结束)", ppOutboundOrder.getOrderStatus());
            map.put("优先级", ppOutboundOrder.getPriority());
            map.put("创建时间", ppOutboundOrder.getCreateTime());
            map.put("创建人", ppOutboundOrder.getCreateBy());
            map.put("开始时间", ppOutboundOrder.getStartTime());
            map.put("结束时间", ppOutboundOrder.getEndTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional
    public void startOrder(int orderId) {
        ppOutboundOrderRepository.startOrder(LocalDateTime.now(),orderId);
    }

    @Override
    @Transactional
    public void finishOrder(int orderId) {
        ppOutboundOrderRepository.finishOrder(LocalDateTime.now(),orderId);

    }
}